package cn.jy.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;
import java.util.UUID;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    protected UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
        userDetailsManager.createUser(User.withUsername("root").password("{noop}123").roles("admin").build());
        return userDetailsManager;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .rememberMe()//开启rememberMe功能
//                .key(UUID.randomUUID().toString()) //key
//                .alwaysRemember(true) //开启总是记住我
//                .rememberMeServices(rememberMeServices())
//                .rememberMeServices(rememberMeServices()) //定义了rememberme的实现
//                .tokenRepository(new JdbcTokenRepositoryImpl())
                .tokenRepository(persistentTokenRepository())
                .and()
                .logout()
                .and()
                .csrf().disable();

    }


    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);//设置数据源
//        jdbcTokenRepository.setCreateTableOnStartup(true);//开启自动创建表 persisent_logins  如果数据库中有了这张表 则这个代码要删除
        return jdbcTokenRepository;
    }

//    @Bean
//    public RememberMeServices rememberMeServices(){ //基于内存的实现
//        return new PersistentTokenBasedRememberMeServices(UUID.randomUUID().toString(),userDetailsService(),new InMemoryTokenRepositoryImpl());
//    }

//    @Bean
//    public RememberMeServices rememberMeServices(){ // 基于数据源的实现
//        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
//        //设置database
//        jdbcTokenRepository.setDataSource(dataSource);
//        return new PersistentTokenBasedRememberMeServices(UUID.randomUUID().toString(),userDetailsService(),jdbcTokenRepository);
//    }

//    @Bean
//    public RememberMeServices rememberMeServices(){
//        return new PersistentTokenBasedRememberMeServices(
//                "key",//参数 1：自定义一个生成令牌 key 默认UUID
//                userDetailsService(),//参数 2：认证数据源
//                new InMemoryTokenRepositoryImpl()//参数 3：令牌存储方式
//        );
//    }
}
